class Solution {
public:
    bool isValidBST(TreeNode* root) 
    {
        if(root == NULL)
            return true;
        if(root -> left == NULL && root -> right == NULL)
            return true;
        
        if(root -> left != NULL)
        {
            TreeNode *r = root -> left;
            
            while(r -> right != NULL)
            {
                r = r -> right;    
            }
           
            if(r -> val >= root -> val)
                return false;  
        }
        
        if(root -> right != NULL)
        {
            TreeNode *l = root -> right;
            
            while(l -> left != NULL)
            {
                l = l -> left;
            }
            
            if(l -> val <= root -> val)
                return false;
        }

        return true;   
    }
};